package com.sockettest.QQChatv1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.*;
import java.net.Socket;

public class ClientChatMain extends JFrame{
    public static void main(String[] args) {

        new ClientChatMain();
    }
    //文本区域
    private JTextArea jta;
    //滚动条
    private JScrollPane jsp;
    //面板
    private JPanel jp;
    //文本框
    private JTextField jtf;
    //按钮
    private JButton jb;
    private  BufferedWriter bufferedWriter;

    public ClientChatMain(){
        jta = new JTextArea();
        jta.setEditable(false);
        jsp = new JScrollPane(jta);
        jp= new JPanel();
        jtf = new JTextField(10);
        jb = new JButton("发送");

        jp.add(jtf);
        jp.add(jb);

        //将滚动条，面板全添加到窗体中
        this.add(jsp, BorderLayout.CENTER);
        this.add(jp,BorderLayout.SOUTH);

        //设置标题，大小，位置，关闭，是否可见
        this.setTitle("QQ聊天-客户端");
        this.setSize(500,500);
        this.setLocation(300,0);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        jb.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("发送按钮被点击");
                //获取文本内容
                sendMsg();
            }
        });
        jtf.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {

                if(e.getKeyCode()==KeyEvent.VK_ENTER){
                    System.out.println(e);
                    //发送数据
                    sendMsg();
                }
            }
            @Override
            public void keyReleased(KeyEvent e) {

            }
        });
        //客户端start
        try {
            Socket socket = new Socket("127.0.0.1", 8080);

            //写出数据
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

            //读入数据
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String line = null;
            while ((line = bufferedReader.readLine()) != null){
                jta.append(line + System.lineSeparator());
            }
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //客户端end
    }
    private void sendMsg(){
        //获取文本内容
        String text = jtf.getText();
        text = "客户端说："+text;
        jta.append(text+System.lineSeparator());
        try {
            bufferedWriter.write(text);
            bufferedWriter.newLine();
            bufferedWriter.flush();
            //清空文本框
            jtf.setText("");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
